package com.ruoyi.zlwx.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zlwx.domain.Zlwx;
import com.ruoyi.zlwx.domain.ZlwxFj;
import com.ruoyi.zlwx.mapper.ZlwxMapper;
import com.ruoyi.zlwx.service.IZlwxService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 专利文献Service业务层处理
 *
 * @author huacai
 * @date 2024-10-16
 */
@Service
public class ZlwxServiceImpl implements IZlwxService {
    @Resource
    private ZlwxMapper zlwxMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询专利文献
     *
     * @param zlwxId 专利文献主键
     * @return 专利文献
     */
    @Override
    public Zlwx selectZlwxByZlwxId(String zlwxId) {
        return zlwxMapper.selectZlwxByZlwxId(zlwxId);
    }

    /**
     * 查询专利文献列表
     *
     * @param zlwx 专利文献
     * @return 专利文献
     */
    @Override
    public List<Zlwx> selectZlwxList(Zlwx zlwx) {
        return zlwxMapper.selectZlwxList(zlwx);
    }

    /**
     * 新增专利文献
     *
     * @param zlwx 专利文献
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZlwx(Zlwx zlwx) {
        zlwx.setCreateTime(DateUtils.getNowDate());
        zlwx.setIdWhere("存在");
        int rows = zlwxMapper.insertZlwx(zlwx);
        insertZlwxFj(zlwx);
        return rows;
    }

    /**
     * 批量新增专利文献
     *
     * @param zlwxs 专利文献List
     * @return 结果
     */
    @Override
    public int batchInsertZlwx(List<Zlwx> zlwxs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(zlwxs)) {
            try {
                for (int i = 0; i < zlwxs.size(); i++) {
                    int row = zlwxMapper.insertZlwx(zlwxs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == zlwxs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改专利文献
     *
     * @param zlwx 专利文献
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZlwx(Zlwx zlwx) {
        zlwx.setUpdateTime(DateUtils.getNowDate());
//        zlwxMapper.deleteZlwxFjByZlwxId(zlwx.getZlwxId());
        insertZlwxFj(zlwx);
        return zlwxMapper.updateZlwx(zlwx);
    }

    /**
     * 批量删除专利文献
     *
     * @param zlwxIds 需要删除的专利文献主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZlwxByZlwxIds(String[] zlwxIds) {
        zlwxMapper.deleteZlwxFjByZlwxIds(zlwxIds);
        return zlwxMapper.deleteZlwxByZlwxIds(zlwxIds);
    }

    /**
     * 删除专利文献信息
     *
     * @param zlwxId 专利文献主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZlwxByZlwxId(String zlwxId) {
        zlwxMapper.deleteZlwxFjByZlwxId(zlwxId);
        return zlwxMapper.deleteZlwxByZlwxId(zlwxId);
    }

    /**
     * 新增附件
     *
     * @param zlwxFj
     * @return
     */
    @Override
    public int insertZlwxFjByZlwxId(ZlwxFj zlwxFj) {
        return zlwxMapper.insertZlwxFjByZlwxId(zlwxFj);
    }

    /**
     * 根据专利文献ID查询附件列表
     *
     * @param zlwxFj
     * @return
     */
    @Override
    public List<ZlwxFj> selectZlwxFjList(ZlwxFj zlwxFj) {
        return zlwxMapper.selectZlwxFjList(zlwxFj);
    }

    /**
     * 批量删除专利文献附件
     *
     * @param fjIds 需要删除的专利文献附件主键
     * @return 结果
     */
    @Override
    public int deleteZlwxFjByFjIds(Integer[] fjIds) {
        return zlwxMapper.deleteZlwxFjByFjIds(fjIds);
    }

    /**
     * 新增专利文献附件信息
     *
     * @param zlwx 专利文献对象
     */
    public void insertZlwxFj(Zlwx zlwx) {
        List<ZlwxFj> zlwxFjList = zlwx.getZlwxFjList();
        String zlwxId = zlwx.getZlwxId();
        if (StringUtils.isNotNull(zlwxFjList)) {
            List<ZlwxFj> list = new ArrayList<ZlwxFj>();
            for (ZlwxFj zlwxFj : zlwxFjList) {
                zlwxFj.setZlwxId(zlwxId);
                list.add(zlwxFj);
            }
//            if (list.size() > 0) {
//                zlwxMapper.batchZlwxFj(list);
//            }
        }
    }
}
